<?php

/**
 * Created by JiangJiaCai.
 * User: Administrator
 * Date: 2017/7/7 0007
 * Time: 14:33
 */
namespace ext\dao;

use App\Models\QdcsConfig;
use ext\lib\BaseDaoImpl;

class ConfigDaoImpl extends BaseDaoImpl
{
    /**
     * 获取配置项
     * @param string $key
     * @return string
     */
    public function get(string $key) : string {
        try{
            $config = QdcsConfig::whereConfigKey($key)->first()->config_val;
        }catch (\Exception $e){
            $config = null;
            logger()->error($e->getMessage());
        }
        return is_null($config) ? "" : $config;
    }

    /**
     * 设置配置项
     * @param string $key
     * @param mixed $val
     */
    public function set(string $key ,  $val){
        $xhsConfig = new QdcsConfig();
        $xhsConfig->config_key = $key;
        $xhsConfig->config_val = $val;
        $xhsConfig->save();
    }

    /**
     * 删除配置
     * @param string $key
     */
    public function del(string  $key){
        $xhsConfig = QdcsConfig::whereConfigKey($key);
        $xhsConfig->delete();
    }

    /**
     * 每次调用人数加一
     */
    public function quesionJoinNumCtr(){
        $xhsConfig = QdcsConfig::whereConfigKey('quesionJoinNum')->first();
        $xhsConfig->config_val += 1;
        $xhsConfig->save();
    }
}